<?php

declare(strict_types=1);

namespace app\controller\develop;

use app\BaseController;
use app\service\develop\DistrictService;

class District extends BaseController
{
    protected $districtService;

    public function initialize()
    {
        $this->districtService = make(DistrictService::class);
    }
    /**
     * 地区列表
     */
    public function list()
    {
        return $this->success($this->districtService->list($this->paging()));
    }
    /**
     * 地区编辑
     */
    public function edit()
    {
        $data = $this->request->post();
        $this->validate($data, [
            'parent_id' => 'integer|egt:0',
            'name' => 'require|length:1,30',
            'province' => 'require|length:1,30',
            'lng' => 'gt:0',
            'lat' => 'gt:0',
            'fullname' => 'require|length:1,50',
        ]);
        $res = $this->districtService->edit($data, (int)$data['id']);
        return $res ? $this->success() : $this->error('保存失败');
    }
    /**
     * 地区状态
     */
    public function status()
    {
        $param = $this->request->post();
        return $this->success($this->districtService->status($param['id']));
    }
    /**
     * 地区详情
     */
    public function info()
    {
        $param = $this->request->get();
        return $this->success($this->districtService->info($param['id']));
    }
    /**
     * 地区导入
     */
    public function import()
    {
        $file = $this->request->file('file');
        return $this->success($this->districtService->import($file));
    }
    /**
     * 地区选项
     */
    public function option()
    {
        $param = $this->request->get();
        $parentId = $param['parent_id'] ?? 0;
        return $this->success($this->districtService->option((int)$parentId));
    }
}
